package com.hylanda.pb.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hylanda.pb.model.ReportModel;
import com.hylanda.pb.service.PowerBiService;
import com.hylanda.pb.service.PowerBiTokenService;
import com.hylanda.pb.service.ReportModelService;
import com.hylanda.pb.service.common.ConstantHelper;

/**
 * @author zhangy
 * @E-mail:blackoon88@gmail.com
 * @qq:846579287
 * @version created at：2018年1月18日 下午4:16:38 自定义报表模型 包括查询和更新
 */
@Controller
@RequestMapping("/reportmodel")
public class ReportModelController {

	@Autowired
	private ReportModelService reportModelService;
	@Autowired
	private PowerBiTokenService powerBiTokenService;
	@Autowired
	private PowerBiService powerBiService;

	@RequestMapping(value = { "/get/{instanceId}" }, method = RequestMethod.GET)
	public String getPowerbiHtml(HttpServletRequest request,
			HttpServletResponse response,
			@PathVariable("instanceId") String instanceId, ModelMap mode) {
		// 数据库查询报表相关信息
		Map<String, Object> map = reportModelService.getReportModelByInstanceId(instanceId);
		if (map == null) {
			mode.put("info", "输入的instanceID没找到相应的记录");
			return "error";
		}
		// token生成
		String token = powerBiTokenService.genToken(map);
		if (token == null) {
			mode.put("info", "token  生成失败!");
			return "error";
		}
		//displayName 列表
		mode.put("token", token);
		mode.put("pbMap",map);
		return "pb/reportmodel/index";
	}
	
	@RequestMapping(value={"/update/report/model"},method = RequestMethod.POST)
	public @ResponseBody String updateReportModel(HttpServletRequest request,
			HttpServletResponse response,ReportModel model){
		String reportID=powerBiService.getReportIdBydisplayName(model);
		if(reportID==null)
			return ConstantHelper.RESPONSE_FAIL;
		String result=reportModelService.updateReportModel(model,reportID);
		return result;
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return  ImportID CollectionName WorkspaceID AccessKey
	 */
	@RequestMapping(value={"/get/report/model/list"},method = RequestMethod.POST)
	public @ResponseBody JSONObject getModelList(HttpServletRequest request,
			HttpServletResponse response,ReportModel model){
		JSONObject jo=new JSONObject();
		JSONArray reports= powerBiService.getReportIdsBydisplayName(model);
		if(reports==null){
			jo.put("data", ConstantHelper.RESPONSE_FAIL);
		}else{
			jo.put("data", reports);
		}
		return jo;
	}
	
	
}
